package com.ruoyi.web.controller.webContent;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.webContent.domain.PlHostTrade;
import com.ruoyi.webContent.service.IPlHostTradeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 网站交易Controller
 * 
 * @author ruoyi
 * @date 2022-04-18
 */
@Controller
@RequestMapping("/system/trade")
public class PlHostTradeController extends BaseController
{
    private String prefix = "system/trade";

    @Autowired
    private IPlHostTradeService plHostTradeService;

    @RequiresPermissions("system:trade:view")
    @GetMapping()
    public String trade()
    {
        return prefix + "/trade";
    }

    /**
     * 查询网站交易列表
     */
    @RequiresPermissions("system:trade:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlHostTrade plHostTrade)
    {
        startPage();
        List<PlHostTrade> list = plHostTradeService.selectPlHostTradeList(plHostTrade);
        return getDataTable(list);
    }

    /**
     * 导出网站交易列表
     */
    @RequiresPermissions("system:trade:export")
    @Log(title = "网站交易", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlHostTrade plHostTrade)
    {
        List<PlHostTrade> list = plHostTradeService.selectPlHostTradeList(plHostTrade);
        ExcelUtil<PlHostTrade> util = new ExcelUtil<PlHostTrade>(PlHostTrade.class);
        return util.exportExcel(list, "trade");
    }


    /**
     * 修改网站交易
     */
    @GetMapping("/edit/{hostAMarketIdHostBMarketId}")
    public String edit(@PathVariable("hostAMarketIdHostBMarketId") String hostAMarketIdHostBMarketId, ModelMap mmap)
    {
        PlHostTrade plHostTrade = plHostTradeService.selectPlHostTradeById(hostAMarketIdHostBMarketId);
        mmap.put("plHostTrade", plHostTrade);
        return prefix + "/edit";
    }

    /**
     * 修改保存网站交易
     */
    @RequiresPermissions("system:trade:edit")
    @Log(title = "网站交易", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlHostTrade plHostTrade)
    {
        return toAjax(plHostTradeService.updatePlHostTrade(plHostTrade));
    }

    /**
     * 删除网站交易
     */
    @RequiresPermissions("system:trade:remove")
    @Log(title = "网站交易", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(plHostTradeService.deletePlHostTradeByIds(ids));
    }

    /**
     * 获取要做的交易(1.获取后打开购买界面，2.状态变更为已发送)
     */
    @PostMapping("/getTrade")
    @ResponseBody
    public JSONObject getTrade(PlHostTrade plHostTrade, HttpServletResponse response) {
        //解决跨域问题
        response.setHeader("Access-Control-Allow-Origin","*");
        List<PlHostTrade> plHostTrades = plHostTradeService.selectPlHostTradeList(plHostTrade);
        PlHostTrade trade=plHostTrades.get(0);
        trade.setTradeStatus("1");
        plHostTradeService.updatePlHostTrade(trade);
        return (JSONObject)JSONObject.toJSON(trade);
    }

    /**
     * 验证和计算要做的交易(1.两个对手盘获取额度后发回服务器，2.计算出两边购买额，3.状态变更为已验证，
     * 4.死循环等待对手盘验证，5.完成验证后获取做单信息，6.页面完成交易，7.发回做单结果)
     *
     * 根据最大额度计算下单额度
     */
    @PostMapping("/checkTrade")
    @ResponseBody
    public JSONArray checkTrade(PlHostTrade plHostTrade) {
        plHostTrade.setTradeStatus("0");
        List<PlHostTrade> plHostTrades = plHostTradeService.selectPlHostTradeList(plHostTrade);
        for (PlHostTrade trade:plHostTrades) {
            trade.setTradeStatus("1");
            plHostTradeService.updatePlHostTrade(trade);
        }
        return (JSONArray)JSONObject.toJSON(plHostTrades);
    }

    /**
     * 返回做单结果
     */
    @PostMapping("/marketReslut")
    @ResponseBody
    public JSONArray marketReslut(PlHostTrade plHostTrade) {
        plHostTrade.setTradeStatus("0");
        List<PlHostTrade> plHostTrades = plHostTradeService.selectPlHostTradeList(plHostTrade);
        for (PlHostTrade trade:plHostTrades) {
            trade.setTradeStatus("1");
            plHostTradeService.updatePlHostTrade(trade);
        }
        return (JSONArray)JSONObject.toJSON(plHostTrades);
    }
}
